home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++
- Subject: Re: Callback Functions In C++
- Date: 15 Apr 1996 10:46:40 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4kt9eg$etk@news1.h1.usa.pipeline.com>
- References: <4kt0ol$qdt@dub-news-svc-6.compuserve.com>
- NNTP-Posting-Host: 38.8.120.16
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Apr 15, 1996 08:18:26 in article <Re: Callback Functions In C++>,
- '100435.736@compuserve.com (David A. Mair)' wrote:
-
-
- >andy.walsh@ukonline.co.uk wrote:
- >
- >>Can anyone tell me how I could code a callback function that works
- >>inside a class and that I can pass the address of to the waveInOpen
- >>function. It would seem that a member function carries an extra
- >>'this' pointer and so cannot be addressed the same as an ordinary
- >>function. Any ideas? I also tried to use Borland's response table
- >>to handle the messages but it will only respond to a 'SendMessage'
- >>command and not to my low level audio commands.
- >
- >The response you received from Dmitry covers the most important part
- >of the method but I thought it would be worth adding something. If
- >you use a static member function you will not have a this pointer and
- >subsequently you will not be able to access any per instance member
- >variables. A static member can only make use of other static members
- >unless it has a way of obtaining the value of this for a particular
- >instance of the class. You can do this by creating a static array of
- >pointers to the class type, although you will need some other
- >component to ensure that you can find the correct this pointer for the
- >callback that has happened. One way might be to maintain a static
- >array of Window handles with index numbers that match the index
- >numbers of their associated this pointer for the class to handle the
- >callback for that window. This, of course, assumes that the callbacks
- >are multiple and associated with Windows.
- >
-
- PMFJI also, but IMHO, it's counterproductive to try to force C++
- semantics on a straight C paradigm. Let's call a spade a spade
- and deal with it. I other words, rather than try to make a member
- function out of an ordinary callback, make it a straight function
- from which you can call a member, if appropriate. Naturally,
- there are exceptions where a static member function may be the
- best way, but in most instances the following is adequate and
- probably easier to understand and maintain:
-
- Under Motif:
- XtAddCallback(...,foo, (XtPointer)&object);
-
- void foo(Widget, XtPointer calldata, XtPointer)
- {
- MyClass * objptr = (MyClass*)calldata;
- objptr->memberfunc();
- }
-
- Under Windows:
-
- Associate a window with an object:
- SetWindowLong(hWnd, GWL_USERDATA, (long)&object);
-
- In window proc:
- MyClass * objptr = (MyClass*)GetWindowLong(hWnd, GWL_USERDATA);
- objptr->memberfunc();
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-